package cn.tedu.hongbei.pms.controller;

import cn.tedu.commons.pojo.vo.PageData;
import cn.tedu.commons.web.JsonResult;
import cn.tedu.hongbei.pms.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.hongbei.pms.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.hongbei.pms.pojo.vo.*;
import cn.tedu.hongbei.pms.service.IAttributeTemplateService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/attribute-template/")
@Api(tags = "6.属性模板管理模块")//给Api文档加上说明
@Slf4j
//我们添加此注解是为了检查参数是否符合，下面我们的方法参数中对象中添加Valid和Validated效果相同，
// 但是如果要判断单参数需要使用到validated
@Validated
public class AttributeTemplateController {
    @Autowired
    private IAttributeTemplateService iAttributeTemplateService;

    @PostMapping("add-new")//这样加了注解只能允许post请求
//    @RequestMapping(value = "add-new", method = {RequestMethod.POST, RequestMethod.GET}) //我们可以通过这样的方式来让他有get和post请求
//    @RequestMapping(value = "add-new", method = RequestMethod.POST) //这样只能允许post请求，我们的method是一个数组，但是只有一个元素可以省略{}
    @ApiOperation("添加属性模板")//给此请求加上说明
    @ApiOperationSupport(order = 100)//order增加一个序号来方便排序
    public JsonResult addNew(@Valid AttributeTemplateAddNewParam attributeTemplateNewParam, @ApiIgnore HttpSession session){//没有@ApiIgnore会显示关于session的api各种信息
            iAttributeTemplateService.addNew(attributeTemplateNewParam);
            return JsonResult.ok();

    }

    @ApiOperation("根据id删除属性模板")
    @ApiOperationSupport(order = 200)//order增加一个序号来方便排序
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long"),
    })
    @PostMapping("deleteById")
    public JsonResult deleteById(@Range(min = 1, message = "根据ID删除品牌失败，请提交合法的ID值！") @RequestParam Long id){
        iAttributeTemplateService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改属性模板")
    @ApiOperationSupport(order = 300)//order增加一个序号来方便排序
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long"),
    })
    @PostMapping("updateById")
    public JsonResult updateById(@Range(min = 1, message = "根据ID修改属性模板失败，请提交合法的ID值！") @RequestParam Long id, @Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam){
        iAttributeTemplateService.updateById(id, attributeTemplateUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order =420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "查询属性模板列表失败，请提供正确的页码值！") Integer pageNum,
                           String queryType) {
        log.info("开始处理【查询属性模板列表】的请求，页码：{}", pageNum);
//        if (pageNum == null || pageNum < 1) {
//            pageNum = 1;
//        }
//        PageData<AttributeTemplateListItemVO> pageData = iAttributeTemplateService.list(pageNum);
//        if ("all".equals(queryType)){
//            pageData = iAttributeTemplateService.list(pageNum, Integer.MAX_VALUE);
//        }
        Integer newPageNum = pageNum == null ? 1 : pageNum;
        PageData<AttributeTemplateListItemVO> pageData;
        if ("all".equals(queryType)){
            pageData = iAttributeTemplateService.list(newPageNum, Integer.MAX_VALUE);
        } else {
            pageData = iAttributeTemplateService.list(newPageNum);
        }
        return JsonResult.ok(pageData);
    }

    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)//order增加一个序号来方便排序
    @GetMapping("getStandardById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long", example = "1"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询属性模板详情失败，请提交合法的ID值！")
                                      @RequestParam Long id){
        AttributeTemplateStandardVO result = iAttributeTemplateService.getStandardById(id);
        return JsonResult.ok(result);
    }

}
